﻿Imports System.Data
Public Class frm_SalesOrder
    Private dt_SalesOrder As DataTable
    Private currentID As Integer
    Private isAddNew As Boolean = False
    Private Sub btn_Outlet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Outlet.Click
        Dim f_Outlet As New popup_Outlet
        If f_Outlet.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PublicVariables.cur_CardCode = f_Outlet.dg_Outlet.Item(f_Outlet.dg_Outlet.CurrentRowIndex, 0)
            btn_Outlet.Text = f_Outlet.dg_Outlet.Item(f_Outlet.dg_Outlet.CurrentRowIndex, 1)
            btn_Add.Enabled = True
        End If
    End Sub

    Private Sub frm_SalesOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt_SalesOrder = New DataTable
        Dim col As New DataColumn
        col.ReadOnly = False
        col.ColumnName = "itmName"
        col.Caption = "Item"
        dt_SalesOrder.Columns.Add(col)
        dt_SalesOrder.Columns.Add("qty")
        dt_SalesOrder.Columns.Add("price")
        dt_SalesOrder.Columns.Add("total")
        Dim colID As New DataColumn
        colID.DataType = System.Type.GetType("System.Int32")
        dt_SalesOrder.Columns.Add("ID")
        'dg_SalesOrder.DataSource = dt_SalesOrder
        'dt_SalesOrder.Rows.Add("Hitachi", "2", "500", "1000")
        'dt_SalesOrder.Rows.Add("IT0001 - Sony Vaio", "3", "300", "900")
        'arrangeData()
        btn_Add.Enabled = False
        btn_Save.Enabled = False
    End Sub

    Dim newUC As uc_ItemDetail
    Private Sub btn_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Add.Click
        newUC = New uc_ItemDetail
        newUC.Tag = dt_SalesOrder.Rows.Count 'ID of 
        currentID = dt_SalesOrder.Rows.Count
        AddHandler newUC.FinishUpdateItem, AddressOf UpdateDataTable
        newUC.Top = dt_SalesOrder.Rows.Count * newUC.Height
        newUC.btn_Item.Focus()
        pn_Data.Controls.Add(newUC)
        isAddNew = True
        btn_Add.Enabled = False
    End Sub

    Private Sub UpdateDataTable(ByVal sender As Object, ByVal e As EventArgs)
        If isAddNew = True Then
            Dim newRow As DataRow = dt_SalesOrder.NewRow()
            newRow("itmName") = newUC.btn_Item.Text
            newRow("qty") = newUC.txt_Qty.Text
            newRow("price") = newUC.txt_Price.Text
            newRow("total") = newUC.txt_Total.Text
            newRow("ID") = newUC.Tag
            dt_SalesOrder.Rows.Add(newRow)
            btn_Add.Enabled = True
            isAddNew = False
        End If

        'check xem da co ID nay chua, neu co thi update, khong thi them moi
        'If dt_SalesOrder.Select("ID = " + newUC.Tag.ToString()).Length > 0 Then 'already exists
        '    dt_SalesOrder.Rows(currentID)("itmName") = newUC.btn_Item.Text
        '    dt_SalesOrder.Rows(currentID)("qty") = newUC.txt_Qty.Text
        '    dt_SalesOrder.Rows(currentID)("price") = newUC.txt_Price.Text
        '    dt_SalesOrder.Rows(currentID)("total") = newUC.txt_Total.Text
        'Else

        'End If
        'arrangeData()
    End Sub

    Private Sub arrangeData()
        pn_Data.Controls.Clear()
        pn_Data.Controls.Clear()
        Dim x As Integer = 0
        For Each row As DataRow In dt_SalesOrder.Rows
            Dim uc As New uc_ItemDetail
            uc.Top = x
            uc.Left = 0
            uc.Tag = row("ID")
            uc.btn_Item.Text = row("itmName")
            uc.txt_Qty.Text = row("qty")
            uc.txt_Price.Text = row("price")
            uc.txt_Total.Text = row("total")
            pn_Data.Controls.Add(uc)
            x = x + uc.Height
        Next
    End Sub

    Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
       
    End Sub

    Private Sub btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click

    End Sub
End Class